import Vue from "vue";
import Vuex from "vuex";
import storage from "@/utils/storage";

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    verificationKey: "", //获取key表示验证通过
    distributionId:"", //分销员Id 如果当前账户从未登录过时记录
    hasLogin: storage.getHasLogin(),
    userInfo: storage.getUserInfo(),
    uuid: storage.getUuid(),
    token: "",
	chat: uni.getStorageSync('chat') || [],
	loginConfig: uni.getStorageSync('loginConfig') || {},
	tabbar: uni.getStorageSync('tabbar') || [],
	tabbarIndex: uni.getStorageSync('tabbarIndex') || 0,
	robot: uni.getStorageSync('robot') || {},
	modelList: uni.getStorageSync('modelList') || [],
	theme: uni.getStorageSync('appTheme') || 'light',
	model: uni.getStorageSync('model') || {},
	chatLoading: false,
  },
  mutations: {
    login(state, userInfo) {
      state.userInfo = userInfo || {};
      state.userName =
        userInfo.Name || userInfo.Nickname || userInfo.Username || "匿名用户";
      state.hasLogin = true;
    },
    logout(state) {
      state.userName = "";
      state.hasLogin = false;
    },
	setChatLoading(state, data) {
		state.chatLoading = data
	},
	setChat(state, data) {
		// console.log("********setChat**********",state.chat[data.index])
		if (!data) {
			state.chat = []
		} else if (data.index==0 || data.index) {
			state.chat[data.index] = data
		} else {
			state.chat.push(data)
		}
		uni.$emit("setChat",data);
		uni.setStorageSync('chat', state.chat)
	
	},
	setLoginConfig(state, data) {
		state.loginConfig = data
		uni.setStorageSync('loginConfig', data)
	},
	setTheme(state, data) {
		state.theme = data
		uni.setStorageSync('appTheme', data)
	},
	setTabbar(state, data) {
		state.tabbar = data
		uni.setStorageSync('tabbar', data)
	},
	setTabbarIndex(state, data) {
		state.tabbarIndex = data
		uni.setStorageSync('tabbarIndex', data)
	},
	setRobot(state, data) {
		state.robot = data
		uni.setStorageSync('robot', data)
	},
	setModelList(state, data) {
		state.modelList = data
		uni.setStorageSync('modelList', data)
	},
	setModel(state, data) {
		state.model = data
		uni.setStorageSync('model', data)
	},
  },
  actions: {},
});

export default store;
